﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;
using Aspose.Excel;

namespace LenderClearingHouse
{
    public partial class RentProjectionUnit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblApn.Text = Apn;
                lblProperty.Text = PropertyLocation;
            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Session["BackToRentProjection"] = 1;
            Response.Redirect("~/RentProjection.aspx", true);
        }
        
        protected void SqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            User u = Session["User"] as User;
            e.Command.Parameters["@Apn"].Value = Apn;
        }

        protected void grid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            RentProjectionUnitEditControl userControl = (RentProjectionUnitEditControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
            DataAccess da = new DataAccess();
            da.ExecuteProcedure("insertRentProjectionUnit",
                new string[] { "@Apn", "@UnitNumber", "@SquareFootage", "@Bedrooms", "@Bathrooms", "@Rehab" },
                new object[]{ Apn, userControl.edUnitNumber.Text, userControl.edSquareFootage.DbValue, userControl.edBedrooms.SelectedValue,
                    userControl.edBathrooms.Text, userControl.edRehab.DbValue});

        }

        protected void grid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            RentProjectionUnitEditControl userControl = (RentProjectionUnitEditControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
            
            DataAccess da = new DataAccess();
            da.ExecuteProcedure("updateRentProjectionUnit",
                new string[]{ "@RentProjectionUnitId", "@UnitNumber", "@SquareFootage", "@Bedrooms", "@Bathrooms", "@Rehab" },
                new object[]{ (int)editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RentProjectionUnitId"],
	                userControl.edUnitNumber.Text, userControl.edSquareFootage.DbValue, userControl.edBedrooms.SelectedValue,
                    userControl.edBathrooms.Text, userControl.edRehab.DbValue});
        }

        protected void grid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem itm = e.Item as GridDataItem;
            int rentProjectionUnitId = (int)(sender as RadGrid).MasterTableView.DataKeyValues[itm.ItemIndex]["RentProjectionUnitId"];

            DataAccess da = new DataAccess();
            da.ExecuteProcedure("deleteRentProjectionUnit",
              new string[] { "@RentProjectionUnitId" },
              new object[] { rentProjectionUnitId });
        }

        protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                RadComboBox combo = (RadComboBox)item.FindControl("PageSizeComboBox");

                RadComboBoxItem newItem = combo.Items.FindItemByValue("100");
                if (newItem == null)
                {
                    RadComboBoxItem citem = new RadComboBoxItem("100", "100");
                    citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    combo.Items.Add(citem);
                }
                else
                {
                    newItem.Text = "100";
                }

                newItem = combo.Items.FindItemByValue("1000000");
                if (newItem == null)
                {
                    RadComboBoxItem citem = new RadComboBoxItem("ALL", "1000000");
                    citem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    combo.Items.Add(citem);
                }
                else
                {
                    newItem.Text = "ALL";
                }
            }
        }

        public string Apn
        {
            get
            {
                if (Request.QueryString["apn"] == null)
                    return "-1";

                if (!string.IsNullOrEmpty(Request.QueryString["apn"].ToString()))
                    return HttpUtility.UrlDecode(Request.QueryString["apn"].ToString());
                else
                    return "-1";
            }
        }

        public string PropertyLocation
        {
            get
            {
                if (ViewState["PropertyLocation"] == null)
                {
                    ViewState["PropertyLocation"] = "";
                    DataAccess da = new DataAccess();
                    DataTable dt = da.GetDataTable("getRentProjection", new string[] { "@Apn" }, new object[] { Apn });
                    if (dt.Rows.Count > 0)
                        ViewState["PropertyLocation"] = dt.Rows[0]["PropertyLocation"].ToString();
                    return ViewState["PropertyLocation"].ToString();
                }
                else
                    return ViewState["PropertyLocation"].ToString();
            }
        }
    }
}